package com.cgnpc.scp.run.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.achievement.pojo.IdReq;
import com.cgnpc.scp.common.bean.CreateFileInfo;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.service.AppConstantService;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.run.mybatis.entity.ProjectInfo;
import com.cgnpc.scp.run.pojo.ProjectImplementationInfoVo;
import com.cgnpc.scp.run.pojo.ProjectUndertakeInfoVo;
import com.cgnpc.scp.run.service.ProjectInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Locale;
import java.util.Map;

/**********************************
 * 用途说明: 项目列表
 * 作者姓名: zhangyibing
 * 创建时间: 2020年9月13日15:51:35
 ***********************************/
@RestController
@RequestMapping("/run/projectInfo")
public class ProjectInfoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MarketInfoController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private ProjectInfoService projectInfoService;

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private AppConstantService appConstantService;

    /**********************************
     * 用途说明: 查询项目列表
     * 参数说明 projectInfo 查询实体
     * 返回值说明: 项目列表
     ***********************************/
    @ApiOperation(value="查询项目列表")
    @UBA(module = "调试经营管理", action = "查询同心圆项目列表", channel = OperatorType.Page)
    @RequestMapping(value = "/listPage", method = {RequestMethod.POST})
    public AjaxResult listPage(HttpServletRequest request, @RequestBody Map<String, Object> params){
        try {
            MyPageResult<ProjectInfo> result = projectInfoService.queryProjectInfoPageList(params);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,result);
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController listPage error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 项目信息保存
     * 参数说明 projectInfo 实体
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="保存项目信息")
    @UBA(module = "调试经营管理", action = "保存项目信息", channel = OperatorType.Button)
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public AjaxResult save(HttpServletRequest request, @RequestBody ProjectInfo projectInfo){
        try {
            return projectInfoService.saveProjectInfo(projectInfo);
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController save error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 更新项目
     * 参数说明 projectInfo 实体
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="更新项目")
    @UBA(module = "调试经营管理", action = "更新同心圆项目", channel = OperatorType.Button)
    @RequestMapping(value = "/updateProjectInfoById", method = {RequestMethod.POST})
    public AjaxResult updateProjectInfoById(HttpServletRequest request, @RequestBody ProjectInfo projectInfo){
        try {
            return projectInfoService.updateProjectInfoById(projectInfo);
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController updateProjectInfoById error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 编辑项目
     * 参数说明 IdReq 实体
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="编辑项目")
    @UBA(module = "调试经营管理", action = "编辑同心圆项目", channel = OperatorType.Button)
    @RequestMapping(value = "/getProjectInfoById", method = {RequestMethod.POST})
    public AjaxResult getProjectInfoById(HttpServletRequest request, @RequestBody IdReq req){
        try {
            return projectInfoService.getProjectInfoById(req.getId());
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController getProjectInfoById error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 查看/编辑承揽详情
     * 参数说明 IdReq 实体
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="查看/编辑承揽详情")
    @UBA(module = "调试经营管理", action = "查看项目承揽详情", channel = OperatorType.Button)
    @RequestMapping(value = "/getProjectUndertakeInfoById", method = {RequestMethod.POST})
    public AjaxResult getProjectUndertakeInfoById(HttpServletRequest request, @RequestBody IdReq req) throws InstantiationException, IllegalAccessException {
        try {
            ProjectUndertakeInfoVo vo = projectInfoService.getProjectUndertakeInfoById(req.getId());
            if (vo != null)
                return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,vo);
            else
                return AjaxResultUtil.createI18nDefaultError(request,messageSource,"查询失败，没有找到对应记录!");
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController getProjectUndertakeInfoById error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 查看/编辑实施详情
     * 参数说明 IdReq 实体
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="查看/编辑实施详情")
    @UBA(module = "调试经营管理", action = "查看实施详情", channel = OperatorType.Button)
    @RequestMapping(value = "/getProjectImplementationInfoById", method = {RequestMethod.POST})
    public AjaxResult getProjectImplementationInfoById(HttpServletRequest request, @RequestBody IdReq req) throws InstantiationException, IllegalAccessException {
        try {
            ProjectImplementationInfoVo vo = projectInfoService.getProjectImplementationInfoById(req.getId());
            if (vo != null)
                return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,vo);
            else
                return AjaxResultUtil.createI18nDefaultError(request,messageSource,"查询失败，没有找到对应记录!");
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController getProjectImplementationInfoById error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 删除项目
     * 参数说明 IdReq 实体
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="删除项目")
    @UBA(module = "调试经营管理", action = "删除项目", channel = OperatorType.Button)
    @RequestMapping(value = "/remove", method = {RequestMethod.POST})
    public AjaxResult remove(HttpServletRequest request, @RequestBody IdReq req){
        try {
            return projectInfoService.remove(req.getId());
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController remove error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 调试经营管理-项目管理-导出Excel
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试经营管理-项目管理-导出Excel")
    @UBA(module = "调试经营管理", action = "项目导出 Excel", channel = OperatorType.Button)
    @RequestMapping(value = "/exportExcel", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult exportExcel(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        try {
            CreateFileInfo createFileInfo = projectInfoService.exportExcel(params);
            return AjaxResultUtil.createSuccessMessage(createFileInfo);
        } catch (Exception e) {
            LOGGER.error("ProjectInfoController exportExcel error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 项目管理 - 导入
     * 参数说明: MultipartFile file
     * 返回值说明: AjaxResult
     ***********************************/
    @ApiOperation(value = "项目管理 - 导入")
//    @UBA(module = "调试经营管理", action = "项目导入 Excel", channel = OperatorType.Button)
    @RequestMapping(value = "/importFromExcel", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult importFromExcel(MultipartFile file) {
        try{
            boolean flag = projectInfoService.importFromExcel(file);
            if(flag){
                return AjaxResult.success();
            }else{
                return AjaxResult.error("批量导入失败，请检查");
            }
        }catch (Exception e){
            LOGGER.error("ProjectInfoController importFromExcel error : {}",e.getMessage(),e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 项目管理 - 下载导入模板
     * 参数说明 file
     * 返回值说明:
     ***********************************/
    @ApiOperation(value = "项目管理 - 下载导入模板")
    @RequestMapping(value = "/getTemplate", method = {RequestMethod.POST})
    public void uploadTemp(){
        try{
            projectInfoService.uploadTemp();
        }catch (Exception e){
            LOGGER.error("ProjectInfoController getTemplate error : {}",e.getMessage(),e);
        }
    }

    /**********************************
     * 用途说明: 项目管理 - 通知商务经理（发送邮件）
     * 参数说明
     * 返回值说明:
     ***********************************/
    @ApiOperation(value = "项目管理 - 通知商务经理（发送邮件）")
    @RequestMapping(value = "/notifyBusiness", method = {RequestMethod.POST})
    public AjaxResult notifyBusiness(HttpServletRequest request,  @RequestBody Map<String, Object> params){
        try{
            String projectId = params.get("projectId").toString();
            return AjaxResultUtil.createI18nErrorDefaultMessage(request, messageSource,projectInfoService.notifyBusiness(projectId) );
        }catch (Exception e){
            LOGGER.error("ProjectInfoController notifyBusiness error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nErrorDefaultMessage(request, messageSource, e.getMessage());
        }
    }

}
